#include <ActionManager.h>
#include <iostream>
#include <EDesignerCore.h>
ActionManager::ActionManager()
{
	m_menuBar = new QMenuBar;
	m_toolBar = new QToolBar;
	m_hasBuildDefault = false;
    buildDefault();
}

ActionManager::~ActionManager()
{

}

QMenuBar* ActionManager::getMenuBar()
{
	return m_menuBar;
}

QToolBar* ActionManager::getToolBar()
{
	return m_toolBar;
}

void ActionManager::buildDefault()
{
	menuBarAddMenu("File");
    menuAddAction("File", "New", "resource/action_new.png");
    menuAddAction("File", "Open", "resource/action_open.png");
    menuAddAction("File", "Save", "resource/action_save.png");

    menuBarAddMenu("Edit");
    menuAddAction("Edit", "Redo", "resource/action_redo.png");
    menuAddAction("Edit", "Undo", "resource/action_undo.png");
    menuAddAction("Edit", "Copy");
    menuAddAction("Edit", "Paste");
    menuAddAction("Edit", "Delete");
    //menuAddAction("Edit", "New Object");

    menuBarAddMenu("Simulation");
    menuAddAction("Simulation", "RunInSingleThread");
    menuAddAction("Simulation", "RunInMainThread");
    menuAddAction("Simulation", "Start-Sim", "resource/action_start_sim.png");
    menuAddAction("Simulation", "Pause-Sim", "resource/action_pause_sim.png");
    menuAddAction("Simulation", "Stop-Sim", "resource/action_stop_sim.png");


    menuBarAddMenu("Windows");

    toolBarAddAction("New");
    toolBarAddAction("Open");
    toolBarAddAction("Save");
 toolBarInsertSeparator();
    toolBarAddAction("Undo");
    toolBarAddAction("Redo");
toolBarInsertSeparator();
    toolBarAddAction("Start-Sim");
    toolBarAddAction("Pause-Sim");
    toolBarAddAction("Stop-Sim");


    //set simulation button initial state
    QAction* startActionPtr = findActionByText("Start-Sim");
    QAction* pauseActionPtr = findActionByText("Pause-Sim");
    QAction* stopActionPtr = findActionByText("Stop-Sim");
    startActionPtr->setEnabled(true);
    pauseActionPtr->setEnabled(false);
    stopActionPtr->setEnabled(false);

}

void ActionManager::setUnableWhenUIStarted()
{
    for (int i = 0; i < m_actions.size(); ++i)
    {
        if (m_actions.at(i)->text() == "New" || m_actions.at(i)->text() == "Open")
        {

        }
        else
        {
            m_actions.at(i)->setEnabled(false);
        }
    }
}

void ActionManager::setEnableAfterSelectFile()
{
    for (int i = 0; i < m_actions.size(); ++i)
    {
        if (m_actions.at(i)->text() == "New" || m_actions.at(i)->text() == "Open")
        {

        }
        else
        {
            m_actions.at(i)->setEnabled(true);
        }
    }
}

void ActionManager::actionTriggered(bool checked)
{
    QObject* sender = QObject::sender();
    QAction* action_ptr = qobject_cast<QAction*>(sender);

    if (action_ptr != NULL)
    {
        std::cout<<"action triggered:"<<action_ptr->text().toStdString()<<std::endl;
        if(action_ptr->text() == "Undo")
        {
            EDesignerCore::instance()->undo();
        }
        else if(action_ptr->text() == "Redo")
        {
            EDesignerCore::instance()->redo();
        }
        else if(action_ptr->text() == "New")
        {
            EDesignerCore::instance()->newFile();
        }
        else if(action_ptr->text() == "Save")
        {
            EDesignerCore::instance()->saveFile();
        }
        else if(action_ptr->text() == "Open")
        {
            EDesignerCore::instance()->loadFileByDialog();
        }
        else if(action_ptr->text() == "RunInSingleThread")
        {
            EDesignerCore::instance()->simulationRunAsSingleThread();
        }
        else if(action_ptr->text() == "RunInMainThread")
        {
            EDesignerCore::instance()->simulationRunInMainThread();
        }
        else if(action_ptr->text() == "Start-Sim")
        {
            EDesignerCore::instance()->simulationStart();
        }
        else if(action_ptr->text() == "Pause-Sim")
        {
            EDesignerCore::instance()->simulationPause();
        }
        else if(action_ptr->text() == "Stop-Sim")
        {
            EDesignerCore::instance()->simulationStop();
        }
    }
}

void ActionManager::connectAction(QAction *action_ptr)
{
    QObject::connect(action_ptr, SIGNAL(triggered(bool)), this, SLOT(actionTriggered(bool)));

}

void ActionManager::menuAddAction(const QString& menu_text, const QString& action_text , const QString &action_icon)
{
	QMenu* menu_ptr = findMenuByText(menu_text);
	if (menu_ptr == NULL)
	{
		return ;
	}
	
	QAction* action_ptr = findActionByText(action_text);
	if (action_ptr == NULL)
	{
		action_ptr = new QAction(action_text, menu_ptr);
        connectAction(action_ptr);
        m_actions.push_back(action_ptr);

        action_ptr->setIcon( QIcon(action_icon) );
	}

    menu_ptr->addAction(action_ptr);
}

void ActionManager::menuAddAction(const QString &menu_text, QAction *action_ptr)
{
    QMenu* menu_ptr = findMenuByText(menu_text);
    if (menu_ptr == NULL)
    {
        return ;
    }


    if (action_ptr != NULL)
    {
        menu_ptr->addAction(action_ptr);
    }


}

void ActionManager::menuBarAddMenu( const QString& menu_text )
{
	QMenu* menu_ptr = new QMenu(menu_text);
	m_menus.push_back(menu_ptr);
	m_menuBar->addMenu(menu_ptr);
}

void ActionManager::menuAddMenu( const QString& menu_text )
{

}

QAction* ActionManager::findActionByText(const QString& text)
{
	for (int i = 0; i < m_actions.size(); ++i)
	{
		if (m_actions.at(i)->text() == text)
		{
			return m_actions.at(i);
		}
	}
	return NULL;
}

QMenu* ActionManager::findMenuByText(const QString& text)
{
	for (int i = 0; i < m_menus.size(); ++i)
	{
		if (m_menus.at(i)->title() == text)
		{
			return m_menus.at(i);
		}
	}
	return NULL;
}

void ActionManager::toolBarAddAction( const QString& text )
{
	QAction* action_ptr = findActionByText(text);
	if (action_ptr == NULL)
	{
		action_ptr = new QAction(text, m_toolBar);
	}
    m_toolBar->addAction(action_ptr);
}

void ActionManager::toolBarInsertSeparator()
{

    if(m_actions.size() > 0)
    {
        m_toolBar->insertSeparator(m_actions.at(m_actions.size()-1));
    }
}
